/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package daos;

import businessObjects.Pago;
import daos.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import businessObjects.Pauta;

/**
 *
 * @author williamprado
 */
public class PagoDAO {

    public PagoDAO() {
        //emf = Persistence.createEntityManagerFactory("Adz-ejbPU");
    }
    //private EntityManagerFactory emf = null;

    /*public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }*/

    /*public void create(Pago pago) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pauta idPauta = pago.getIdPauta();
            if (idPauta != null) {
                idPauta = em.getReference(idPauta.getClass(), idPauta.getIdPauta());
                pago.setIdPauta(idPauta);
            }
            em.persist(pago);
            if (idPauta != null) {
                idPauta.getPagoCollection().add(pago);
                idPauta = em.merge(idPauta);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void create(Pago pago, EntityManager em) throws Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Pauta idPauta = pago.getIdPauta();
            if (idPauta != null) {
                idPauta = em.getReference(idPauta.getClass(), idPauta.getIdPauta());
                pago.setIdPauta(idPauta);
            }
            em.persist(pago);
            if (idPauta != null) {
                idPauta.getPagoCollection().add(pago);
                idPauta = em.merge(idPauta);
            }
            //em.getTransaction().commit();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public void edit(Pago pago) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pago persistentPago = em.find(Pago.class, pago.getIdPago());
            Pauta idPautaOld = persistentPago.getIdPauta();
            Pauta idPautaNew = pago.getIdPauta();
            if (idPautaNew != null) {
                idPautaNew = em.getReference(idPautaNew.getClass(), idPautaNew.getIdPauta());
                pago.setIdPauta(idPautaNew);
            }
            pago = em.merge(pago);
            if (idPautaOld != null && !idPautaOld.equals(idPautaNew)) {
                idPautaOld.getPagoCollection().remove(pago);
                idPautaOld = em.merge(idPautaOld);
            }
            if (idPautaNew != null && !idPautaNew.equals(idPautaOld)) {
                idPautaNew.getPagoCollection().add(pago);
                idPautaNew = em.merge(idPautaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = pago.getIdPago();
                if (findPago(id) == null) {
                    throw new NonexistentEntityException("The pago with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void edit(Pago pago, EntityManager em) throws NonexistentEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Pago persistentPago = em.find(Pago.class, pago.getIdPago());
            Pauta idPautaOld = persistentPago.getIdPauta();
            Pauta idPautaNew = pago.getIdPauta();
            if (idPautaNew != null) {
                idPautaNew = em.getReference(idPautaNew.getClass(), idPautaNew.getIdPauta());
                pago.setIdPauta(idPautaNew);
            }
            pago = em.merge(pago);
            if (idPautaOld != null && !idPautaOld.equals(idPautaNew)) {
                idPautaOld.getPagoCollection().remove(pago);
                idPautaOld = em.merge(idPautaOld);
            }
            if (idPautaNew != null && !idPautaNew.equals(idPautaOld)) {
                idPautaNew.getPagoCollection().add(pago);
                idPautaNew = em.merge(idPautaNew);
            }
            //em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = pago.getIdPago();
                if (findPago(id, em) == null) {
                    throw new NonexistentEntityException("The pago with id " + id + " no longer exists.");
                }
            }
            throw ex;
        }
    }

    /*public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pago pago;
            try {
                pago = em.getReference(Pago.class, id);
                pago.getIdPago();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The pago with id " + id + " no longer exists.", enfe);
            }
            Pauta idPauta = pago.getIdPauta();
            if (idPauta != null) {
                idPauta.getPagoCollection().remove(pago);
                idPauta = em.merge(idPauta);
            }
            em.remove(pago);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }*/

    public void destroy(Integer id, EntityManager em) throws NonexistentEntityException, Exception {
        //EntityManager em = null;
        try {
            //em = getEntityManager();
            //em.getTransaction().begin();
            Pago pago;
            try {
                pago = em.getReference(Pago.class, id);
                pago.getIdPago();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The pago with id " + id + " no longer exists.", enfe);
            }
            Pauta idPauta = pago.getIdPauta();
            if (idPauta != null) {
                idPauta.getPagoCollection().remove(pago);
                idPauta = em.merge(idPauta);
            }
            em.remove(pago);
            //em.getTransaction().commit();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public List<Pago> findPagoEntities() {
        return findPagoEntities(true, -1, -1);
    }*/
    public List<Pago> findPagoEntities(EntityManager em) throws Exception {
        return findPagoEntities(true, -1, -1, em);
    }

    /*public List<Pago> findPagoEntities(int maxResults, int firstResult) {
        return findPagoEntities(false, maxResults, firstResult);
    }*/

    public List<Pago> findPagoEntities(int maxResults, int firstResult, EntityManager em) throws Exception {
        return findPagoEntities(false, maxResults, firstResult, em);
    }

    /*private List<Pago> findPagoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Pago as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }*/

    private List<Pago> findPagoEntities(boolean all, int maxResults, int firstResult, EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Pago as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } catch(Exception e) {
            throw e;
        }
    }

    /*public Pago findPago(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Pago.class, id);
        } finally {
            em.close();
        }
    }*/

    public Pago findPago(Integer id, EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            return em.find(Pago.class, id);
        } catch(Exception e) {
            throw e;
        }
    }

    /*public int getPagoCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Pago as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }*/

    public int getPagoCount(EntityManager em) throws Exception {
        //EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Pago as o");
            return ((Long) q.getSingleResult()).intValue();
        } catch(Exception e) {
            throw e;
        }
    }

}
